package com.qyhl.module_home.home.panzhou;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_home.R;
import com.qyhl.module_home.R2;
import com.qyhl.module_home.home.fragment2.HomeContract;
import com.qyhl.module_home.home.fragment2.HomePresenterImpl;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.config.AppConfigBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PanZhouHomeFragment extends BaseFragment implements HomeContract.HomeView {
    @BindView(R2.id.homeTabLayout)
    SlidingTabLayout homeTabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;
    @BindView(R2.id.title_layout)
    LinearLayout titleLayout;

    private List<AppConfigBean.ThreeMenus> threeMenus;
    private HomeContract.HomeViewPresenter homeViewPresenter;

    public void setThreeMenus(List<AppConfigBean.ThreeMenus> threeMenus) {
        this.threeMenus = threeMenus;
    }

    public static PanZhouHomeFragment newInstance(List<AppConfigBean.ThreeMenus> threeMenus) {
        PanZhouHomeFragment homeFragment = new PanZhouHomeFragment();
        homeFragment.setThreeMenus(threeMenus);
        return homeFragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_fragment_home_panzhou, null);
    }

    @Override
    protected void create() {
        homeViewPresenter = new HomePresenterImpl(this);
        homeViewPresenter.setConfig(threeMenus);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }

    @Override
    public void initImmersionBar() {
        ImmersionBar.with(PanZhouHomeFragment.this)
                .statusBarDarkFont(false)
                .titleBar(titleLayout)
                .init();

    }

    @Override
    public void setNavigation(ArrayList<Fragment> fragmentList, String[] names) {
        viewPager.setAdapter(new Adapter(getChildFragmentManager(), fragmentList, names));
        homeTabLayout.setViewPager(viewPager);
    }

    @OnClick({R2.id.search_layout, R2.id.more_btn})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.search_layout) {
            RouterManager.router(ARouterPathConstant.SEARCH);
            ActionLogUtils.getInstance().onEvent(ActionConstant.FLOWSCENE, ActionConstant.HOME, ActionConstant.SEARCH);
        } else if (id == R.id.more_btn) {
        }
    }


    class Adapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> fragmentList;
        private String[] names;

        public Adapter(FragmentManager fm, ArrayList<Fragment> fragmentList, String[] names) {
            super(fm);
            this.fragmentList = fragmentList;
            this.names = names;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return names[position];
        }
    }
}
